// https://iai.sh.cn/problem/573
#include <bits/stdc++.h>
using namespace std;
const int NMAX = 1e5 + 5;
typedef long long LL;
int a[NMAX], t[NMAX];
LL f[NMAX];
int main() {
  int n;
  cin >> n;
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
  }
  for (int i = 1; i <= n - 1; i++) {
    cin >> t[i];
  }
  memset(f, 0xaf, sizeof(f));
  f[1] = a[1];
  for (int i = 1; i <= n - 1; i++) {
    f[i + 1] = max(f[i + 1], f[i] + a[i + 1]);
    f[t[i]] = max(f[t[i]], f[i] + a[t[i]]);
  }
  cout << f[n];
  return 0;
}